<template>
  <div>
    <common-head title="分类页" />
    <!-- 主体 -->
    <div class="cart-main">
        <ul class="cart-left">
          <li :class="[{active:index===num}]" v-for="(cate,index) in cates"  @click="changeCates(index)" :key='cate.id'>{{cate.name}}</li>
        </ul>
      <div class="cart-right">
       <div  v-if="!items.length">暂无商品</div>
       <div class="good-item" v-for="(item,index) in items" :key="item.id"  @click="enterDetail(item.id,index)">
          <div class="good-pic">
            <img :src="item.pic" alt="">
          </div>
          <div class="good-desc">
            {{item.name}}
          </div>
          <div class="good-price">
            ￥{{item.minPrice }}
          </div>
        </div>
      </div>
    </div>
    <!-- 底部标签栏 -->
    <van-tabbar
      :route="true"
      active-color="#f66"
    >
      <van-tabbar-item to="/home">
        <span>首页</span>
        <template #icon="props">
          <van-icon class="iconfont" class-prefix="icon" name="home"></van-icon>
        </template>
      </van-tabbar-item>

       <van-tabbar-item to="/cates">
        <span>分类</span>
        <template #icon="props">
          <van-icon class="iconfont" class-prefix="icon" name="fenlei1"></van-icon>
        </template>
      </van-tabbar-item>

       <van-tabbar-item to="/cart">
        <span>购物车</span>
        <template #icon="props">
          <van-icon class="iconfont" class-prefix="icon" name="icon-" ></van-icon>
        </template>
      </van-tabbar-item>

       <van-tabbar-item to="/mine">
        <span>我的</span>
        <template #icon="props">
          <van-icon class="iconfont" class-prefix="icon" name="wode01"></van-icon>
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import CommonHead from '@/components/CommonHead'
import Empty from '@/components/Empty'
import { fetchCarts, fetchItems } from '@api'
export default {
  name: 'Cates',
  data () {
    return {
      cates: [],
      num: 0,
      items: []
    }
  },
  created () {
    this.fetchCarts()
  },
  methods: {
    changeCates (index) {
      this.num = index
      this.fetchItems()
    },
    fetchItems () {
      this.items = []
      fetchItems({ categoryId: this.cates[this.num].id }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
          // console.log(this.items)
        }
      })
    },
    fetchCarts () {
      fetchCarts().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    enterDetail (id,index) {
      // console.log(index,id)
      this.$router.push(`/detail/${id}`)
    }
  },
  components: {
    CommonHead,
    Empty
  }
}
</script>

<style lang="scss" scoped>
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.cart-main{
  display: flex;
  font-size: 13px;
  position: fixed;
  left:0;
  right:0;
  top:46px;
  bottom:50px;
  .cart-left{
    width: 20%;
    background: #ffffff;
    box-sizing: border-box;
    color: #232326;
    cursor: pointer;
    border-right: 1px solid #cccccc;
        box-sizing: border-box;
    li{
      width: 100%;
      height: 48.4px;
      display: flex;
      align-items: center;
      padding: 15px 10px;
      box-sizing: border-box;
      &.active{
        color: red;
        background: #e5e5e5;
      }
    }
  }
  .cart-right{
    background-color: #f7f8f9;
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    .good-item{
      width: 50%;
      // height: 20%;
      img{
        width: 150px;
      }
      .good-desc{
           padding-left: 10px;
           font-size: 13px;
           font-weight: normal;
           text-align: center;
      }
      .good-price{
        padding-left: 10px;
           text-align: center;
      }
    }
  }

}

</style>
